package goulu.bloggy.com.Models;


import goulu.bloggy.com.R;
import goulu.bloggy.com.Helpers.PostHelper;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PostAdapter extends BaseAdapter 
{
	ArrayList<Post> Posts;
	Context context;
	
	
	
	public PostAdapter(Context context, ArrayList<Post> posts) 
	{
		Posts = posts;
		this.context = context;
	}

	@Override
	public int getCount() 
	{
		return Posts.size();
	}

	@Override
	public Object getItem(int pos) 
	{
		return Posts.get(pos);
	}

	@Override
	public long getItemId(int position) 
	{	
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Post currentPost = Posts.get(position);
		
		if(convertView == null)
		{
			LayoutInflater inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inf.inflate(R.layout.postitemview, null);
		}	
		//Setting moodimage
		ImageView moodImage = (ImageView)convertView.findViewById(R.id.postitemMoodImage);
		moodImage.setImageResource(PostHelper.getMoodResourceId(currentPost.getMood()));
		
		//Setting title
		TextView titleText = (TextView)convertView.findViewById(R.id.postitemPostTitle);
		Log.d("titletext.gettext length", String.valueOf(currentPost.getTitle().length()));
		if(currentPost.getTitle().length() > 15)
		{	
			titleText.setText("Title:"+currentPost.getTitle().toString().substring(0, 15).trim().replace("\n", " ") + "...");
		}
		else
		{
			titleText.setText("Title:" + currentPost.getTitle().toString().trim().replace("\n", " "));
		}
		
		//Setting content
		TextView contentText = (TextView)convertView.findViewById(R.id.postitemPostContent);
		if(currentPost.getContent().length() > 50)
		{
			contentText.setText("Content:" + currentPost.getContent().toString().substring(0, 50).trim().replace("\n", " ") + "...");
		}
		else
		{
			contentText.setText("Content:" + currentPost.getContent().toString().trim().replace("\n", " "));
		}
		
		ImageView publishedImage = (ImageView)convertView.findViewById(R.id.postitemPublishedImage);
		if(currentPost.isPublished())
		{
			publishedImage.setImageResource(R.drawable.boxchecked);
		}
		else
		{
			publishedImage.setImageResource(R.drawable.boxunchecked);
		}
		return convertView;
	}

}
